package com.projectName.www.servlet;

import com.projectName.www.dao.*;
import com.projectName.www.po.Dish;
import com.projectName.www.po.User;
import com.projectName.www.service.CtmService;
import com.projectName.www.service.CtmServiceImpl;
import com.projectName.www.service.UserService;
import com.projectName.www.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/ctmServlet")
public class CtmServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("utf-8");
        //获取函数，相当于baseservlet

        String ctmid = request.getParameter("uid");
        String did = request.getParameter("did");

        System.out.println("did="+did);

        //通过dishid可以查出来它的slr是谁
        String function = request.getParameter("function");
        if("func1".equals(function)){
            fun1(request,response);
        }else if("func2".equals(function)){
            fun2(request,response);
        }else if("func3".equals(function)){
            fun3(ctmid,did);
            fun2(request,response);
        }else if("func4".equals(function)){
            List<String> remarks = fun4(did);

            request.setAttribute("did",did);
            request.setAttribute("uid",ctmid);
            request.setAttribute("remarks",remarks);
            request.getRequestDispatcher("/remark.jsp").forward(request,response);
        }else if("func5".equals(function)){

            String remark=request.getParameter("remark");
            fun5(remark,did);
            request.setAttribute("did",did);
            request.setAttribute("ctmid",ctmid);
            request.setAttribute("remark",remark);
            //重定向到下单页面
            response.sendRedirect(request.getContextPath()+"/dishListServlet");
        }

    }



    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 浏览菜品：可以评价
     */
    void fun1(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException {

        UserService us=new UserServiceImpl();
        List<Dish> dishs = us.findAllDishs();

        request.setAttribute("dishs",dishs);

        request.getRequestDispatcher("/dishlist.jsp").forward(request,response);
    }

    /**
     * 展示菜单
     *
     */

    void fun2(HttpServletRequest request , HttpServletResponse response) throws IOException {
        response.sendRedirect(request.getContextPath()+"/orderServlet");
    }

    /**
     *核心下单功能
     * @param ctmid 客户id
     * @param did -订单id
     */
    void fun3(String ctmid,String did){
       CtmService cs=new CtmServiceImpl();
        cs.makeOrder(ctmid,did);
    }

    /**
     * 看评论
     * @param did
     * @return
     */
    private List<String> fun4( String did) {
        RemarkDao rd=new RemarkDaoImpl();

        return rd.queryRemarkBydid(did);//查找评论返回list

    }
    /**
     * 设置评论
     * @param rmk 评论
     * @param did id
     */
    private void fun5(String rmk, String did) {
        RemarkDao rd=new RemarkDaoImpl();
        rd.setRemark(did,rmk);
    }
}